package com.lg.cms.utils;

import java.io.*;

/**
 * 模板代码读写工具类
 */
public class IOTemplateTypeUtil {
    /**
     * 写文件方法
     * @param htmlText
     * @param templateTypePath
     * @param name
     * @return
     */
    public static boolean writeFile(String htmlText,String templateTypePath,String name){
        File file = new File(templateTypePath);
        if(!file.isDirectory())
            file.mkdir();//创建目录

        File fileDir = new File(file,name+".ftlh");
        if(!fileDir.isFile()){
            try {
                fileDir.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            FileWriter fw = new FileWriter(fileDir);//FileWriter写入文件时不能指定编码格式，编码格式是系统默认的编码格式
            fw.write(htmlText); //向文件中写入字符串
            fw.flush(); //刷新
            fw.close(); //关闭流
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 读取文件
     * @param path
     * @return
     */
    public static String readFile(String path){
        String str,s="";
        try {
            InputStreamReader read = new InputStreamReader(new FileInputStream(path+".ftlh"),"UTF-8");
            BufferedReader in = new BufferedReader(read);

            while ((str = in.readLine()) != null) {
                s+=str+"\n";
            }
            in.close();
            return s;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}
